package util;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

/**
 * @Author: yg
 * @create 2025-01-25 - 16:59
 * @
 */
public class JDBCUtils {
    public static Connection getConnection() throws Exception {
        InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("jdbc.properties");
        Properties prop = new Properties();
        prop.load(is);
        String user = prop.getProperty("username");
        String password = prop.getProperty("password");
        String url = prop.getProperty("url");
        String driver = prop.getProperty("driver");
        Class.forName(driver);
        return DriverManager.getConnection(url, user, password);
    }

    public static void closeResource(Connection conn, Statement ppst){
        try {
            if(conn != null){
                conn.close();
            }
        }catch (Exception e){e.printStackTrace();}
        try {
            if(ppst != null){
                ppst.close();
            }
        }catch (Exception e){e.printStackTrace();}
    }
    public static void closeResource(Connection conn, Statement ppst, ResultSet resultSet){
        JDBCUtils.closeResource(conn, ppst);
        try {
            if(resultSet != null){
                resultSet.close();
            }
        }catch (Exception e){e.printStackTrace();}
    }
}
